Configuring Nginx Directory Structure

A guide to setting up the Default Web Root and Document Root for WordPress.

Understanding the Web Root

Before creating files, it is crucial to understand the distinction between the Default Web Root and the Document Root.

Target Directory Structure

  • var
    • www (Default Web Root)
      • example.com
        • public_html (Document Root)
      • html (Default Nginx page)

Why use public_html?
Isolating your site files inside a public_html subdirectory enhances security. Only files within this folder are accessible via the web server, preventing direct access to system or configuration files located one level up.

Step 1: Installing the 'Tree' Utility

To visualize our directory structure easily, we will install a utility called tree. First, we must update our package list.

sudo apt update sudo apt upgrade
Note on Phasing: If you see a message stating "upgrades have been deferred due to phasing," do not force the upgrade. These packages will update automatically when ready.

Once the system is updated, install the utility:

sudo apt install tree

Step 2: Creating the Directory Structure

We will now create the directories for your domain. We need to navigate to the default web root and create a folder for the domain, followed by the public_html subfolder.

Method A: Step-by-Step Creation

Navigate to the web root and create directories individually:

cd /var/www sudo mkdir example.com cd example.com sudo mkdir public_html

Method B: The Efficient Way (Recommended)

You can create a parent directory and its subdirectory simultaneously using the -p flag. This is faster and cleaner.

cd /var/www sudo mkdir -p example.net/public_html

You can verify the structure using the tree command:

tree

Step 3: Managing Directories

During setup, you may need to rename or delete directories. Here is how to handle these operations safely.

Renaming a Directory

If you made a typo (e.g., example.grow instead of example.org), use the move command. Warning: Do not rename directories after configuring Nginx server blocks, as this will break your site.

sudo mv example.grow example.org

Deleting Directories

If a directory is empty, use rmdir. If it contains files (like the public_html folder), you must use rm with the recursive force flags.

To remove an empty directory:

sudo rmdir example.org

To remove a directory and all its contents:

sudo rm -rf example.com
Caution: The rm -rf command is powerful. There is no "undelete" or recycle bin in the command line. Verify your spelling before pressing enter.

Final Setup Example

For our live example, we are setting up the site expertwp.help. We will create the full path in one command.

cd /var/www sudo mkdir -p expertwp.help/public_html

Your directories are now ready. The next stage in the process will involve configuring Nginx Server Blocks (Virtual Hosts) to serve content from this new location.